复合类型的结构解析
C++ 复合类型 是通过将基础类型与一个 类型修饰符定义的。虽然引用和指针都提供间接访问,但它们在哲学上是截然不同的。一个 引用 (&) 是一个永久的别名——现有对象的昵称。一旦绑定,就不能重新定位。相反,一个 指针 (*) 是内存中一个独立的对象,存储一个十六进制 地址。它可以被重定向到不同的对象,或设置为 nullptr。
可视化内存布局
在代码 int *p1, p2;中,只有 p1 是指针; p2 是一个普通的整数。要让两者都是指针,请使用 int *p1, *p2;。这强调了修饰符属于单独的声明符,而不是基础类型。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following creates a reference correctly?
int &r;
int i = 10; int &r = i;
int &r = 10;
int *&r = &i;
✅ Correct!
Correct! References must be initialized and bound to an object, not a literal or nothing.❌ Incorrect
References are not objects; they must be bound to an existing variable upon declaration.QUESTION 2
In the declaration 'int* p1, p2;', what are the types of p1 and p2?
Both are pointers to int.
p1 is an int, p2 is a pointer to int.
p1 is a pointer to int, p2 is a plain int.
Neither are valid C++.
✅ Correct!
The '*' modifier applies only to p1. The base type is int, but the declarator p2 lacks the modifier.❌ Incorrect
Type modifiers like '*' or '&' attach to the specific variable name, not the base type (int).QUESTION 3
Which operator is used to obtain the memory address of an object?
The asterisk (*)
The ampersand (&)
The arrow (->)
The dot (.)
✅ Correct!
The address-of operator (&) returns the location of an object in memory.❌ Incorrect
The asterisk (*) is used for dereferencing (accessing the value at an address) or declaration.QUESTION 4
What is the result of dereferencing a pointer?
It gives the memory address.
It creates a new reference.
It yields the object to which the pointer points.
It clears the pointer's memory.
✅ Correct!
Dereferencing 'follows' the address to the actual object, allowing you to read or modify it.❌ Incorrect
Dereferencing retrieves the value, while the address-of operator retrieves the location.QUESTION 5
Which statement about references is FALSE?
A reference is not an object.
A reference can be null.
A reference must be initialized.
A reference cannot be redirected to a new object.
✅ Correct!
References must always be bound to a valid object; there is no such thing as a 'null reference'.❌ Incorrect
References are fixed aliases and do not have a null state, unlike pointers.Module Assessment: Pointers, References, and Sales_data
Applying Compound Types and Structs
You are tasked with upgrading a legacy system. You must manage data using custom structures and ensure memory safety through correct pointer and reference usage.
Q
Exercise 2.41: Based on the Sales_data struct (including bookNo, units_sold, and revenue), how would you rewrite a loop to read multiple transactions and update the total? Focus on the main function logic.
Solution:
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
To implement Exercise 2.41, define the Sales_data struct at the top of the file. In main(), declare a 'total' object. Use a while loop with 'std::cin >> total.bookNo >> total.units_sold >> price' where revenue is calculated as 'units_sold * price'. For subsequent entries, check if 'trans.bookNo == total.bookNo'. If true, update total.units_sold and total.revenue; if false, print the current total and reset it to the new transaction.
Q
Consider the following snippet: 'int i = 42; int *p = &i; *p = *p * *p;'. Explain the final value of 'i' and the role of the asterisk in each part of the second line.
Solution:
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.
The final value of 'i' is 1764 (42 squared). In '*p = *p * *p;', the first and second asterisks are dereference operators used to fetch and store values at the address held by p. The third asterisk is the multiplication operator. This demonstrates how pointers allow indirect modification of the original variable 'i'.